<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
// 提示弹窗
import { ElNotification } from 'element-plus'
// 获取当前时间的函数
import { getTime } from '@/utils/time'
const userStore = useUserStore()
const _router = useRouter()
const _route = useRoute()

const isLoading = ref(false)

const loginForms = ref()

const loginForm = reactive({
  username: 'admin',
  password: '111111',
})
// 登录按钮回调
const login = async () => {
  // 通知仓库发登录请求
  // 请求成功，首页展示数据的地方
  // 请求失败，弹出登录失败信息

  // 保证全部表单校验通过才可以发请求
  await loginForms.value.validate()
  // 开始加载
  isLoading.value = true
  try {
    // 保证登录成功
    await userStore.userLogin(loginForm)
    const redirect: any = _route.query.redirect
    _router.push({ path: redirect || '/' })

    // _router.push('/')
    // 登录成功提示信息
    ElNotification({
      type: 'success',
      title: `Hi ${getTime()}好！`,
      message: '欢迎回来',
    })
    isLoading.value = false
  } catch (error) {
    // 登录失败
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    isLoading.value = false
  }
}
// 定义表单校验需要的配置对象
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule:校验规则对象
  // value:表单元素文本内容
  // 函数:如果符合条件callback放行通过即可
  // 如果不符合条件callback 方法，注入错误信息提示

  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassWord = (rule: any, value: any, callback: any) => {
  if (value.length >= 6 && value.length <= 15) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}

const rules = {
  username: [{ validator: validatorUserName, trigger: 'change' }],
  password: [{ validator: validatorPassWord, trigger: 'change' }],
}
</script>
<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
          class="login_form"
        >
          <h1>Hello</h1>
          <h2>管理系统</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :prefix-icon="Lock"
              show-password
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="login"
              type="primary"
              class="login_but"
              size="default"
              :loading="isLoading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-size: cover;
  background: url('@/assets/images/background.jpg') no-repeat;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    border-radius: 10px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }
    .login_but {
      width: 100%;
    }
  }
}
</style>
